<template>
    <div>
        <div class="sub_tabs_c">
            <div class="sub_tabs">
                <p :class="{active: actNav == '1'}" @click="changeNav('1')">
                    团队成员
                </p>
                <p v-if="isOwner" :class="{active: actNav == '2'}" @click="changeNav('2')">
                    新成员
                    <span v-if="applyCnt" class="point">{{applyCnt}}</span>
                </p>
            </div>
        </div>
        <div class="list">
            <div class="m_item" v-for="(item, index) in list" :key="index">
                <div class="avator">
                    <image :src="item.headImg" mode="" />
                </div>
                <div class="detail">
                    <p class="flex-1">{{item.userName}}</p>
                    <template v-if="isOwner && userInfo.userId != item.userId">
                        <em @click="checkApply('open', item)" class="check" v-if="item.applyStatus == '00'">验证</em>
                        <em @click="delUser(item)" class="del" v-if="item.applyStatus == '01'">移除</em>
                        <em class="refused" v-if="item.applyStatus == '02'">已拒绝</em>
                    </template>
                </div>
            </div>
            <pub-list-end :loading="scroll.loading" :max="scroll.max" :noData="scroll.noData"></pub-list-end>
        </div>

        <uni-popup ref="joinCheck" type="center">
            <div class="add_check">
                <div class="title">
                    <i></i>
                    <p>团队添加验证</p>
                    <i class="icon-close"></i>
                </div>
                <div class="content">{{checkItem.userName + '请求加入团队，是否通过验证？'}}</div>
                <div class="btns">
                    <div class="btn" @click.stop="checkApply('refuse')">拒绝添加</div>
                    <div class="btn" @click.stop="checkApply('confirm')">同意添加</div>
                </div>
            </div>
        </uni-popup>
    </div>
</template>

<script>
import { mapState } from 'vuex';

import { getTeamInfo, getTeamUsers, editTeamUser } from '@/request/team'

export default {
    data() {
        return {
            isOwner: false, // 是否是群主
            applyCnt: 0,
            actNav: '1',
            list: [],
            scroll: {
                page: 1,
                size: 30,
                loading: false,
                max: false,
                noData: false,
            },
            checkItem: {}
        }
    },
    computed: {
        ...mapState({
            userInfo: state => state.user.userInfo
        }),
        canEdit() {

        }
    },
    onLoad() {
        this.getTeamInfo()
        this.onRefresh()
    },
    methods: {
        getTeamInfo() {
            getTeamInfo({
                teamId: this.userInfo.teamId
            }).then(res => {
                this.isOwner = res.data.userId == this.userInfo.userId
                this.applyCnt = res.data.applyCnt
            })
        },
        getList(complete) {
            getTeamUsers({
                pageIndex: 1,
                pageSize: 100,
                applyStatuStr: this.actNav == '1' ? '01' : '00,02',
                teamId: this.userInfo.teamId
            }, { complete }).then(res => {
                this.list = res.data.data.list
                this.scroll.max = this.list.length >= res.data.data.total
                this.scroll.noData = res.data.data.total == 0
            })
        },
        // 重头第一页刷新列表
        onRefresh() {
            this.scroll.loading = true
            this.scroll.max = false
            this.scroll.noData = false
            this.scroll.page = 1
            this.shareUserList = []
            this.getList(() => {
                this.scroll.loading = false;
                uni.stopPullDownRefresh()
            })
        },
        // 滚动到底部加载
        onInfinite() {
            if(this.scroll.loading || this.scroll.max) return
            this.scroll.loading = true
            this.scroll.page += 1
            this.getList(() => {
                this.scroll.loading = false
            })
        },
        changeNav(type) {
            this.actNav = type
            this.onRefresh()

        },
        delUser(item) {
            this.$interact.modal({
                title: '提示',
                content: '确定删除该成员吗？',
                showCancel: true,
            }).then(action => {
                if(action.confirm) {
                    editTeamUser({
                        opType: '03',
                        teamId: this.userInfo.teamId,
                        userId: item.userId
                    }).then(res => {
                        this.$interact.toast(res.msg)
                        this.onRefresh()
                    })
                }
            })
        },
        async checkApply(type, item) {

            const opCheck = (opType) => {
                editTeamUser({
                    opType: opType,
                    teamId: this.userInfo.teamId,
                    userId: this.checkItem.userId
                }).then(res => {
                    this.$interact.toast(res.msg)
                    this.$refs.joinCheck.close()
                    this.onRefresh()
                    this.getTeamInfo()
                })
                this.$refs.joinCheck.close()
            }
            if(type == 'open') {
                this.checkItem = item
                this.$refs.joinCheck.open()
            } else if(type == 'confirm') {
                opCheck('01')
            } else if(type == 'refuse') {                
                opCheck('02')
            }


        }

    }
}
</script>

<style lang="less" scoped>
.sub_tabs_c{
    width: 100%;
    background: #fff;
    padding: 20rpx 0;
    .align(center);
    .sub_tabs{
        width: 690rpx;
        height: 80rpx;
        display: flex;
        border-radius: 10rpx;
        background: #e1e1e1;
        display: flex;
        padding: 10rpx;
        p{
            flex: 1;
            height: 100%;
            margin-right: 10rpx;
            border-radius: 6rpx;
            font-size: 28rpx;
            position: relative;
            &:last-child{
                margin-right: 0
            }
            .align(center);
            &.active{
                background: #fff;
            }

            .point{
                position: absolute;
                top: 0rpx;
                left: 195rpx;
                color: #fff;
                padding: 0 10rpx;
                height: 32rpx;
                font-size: 24rpx;
                border-radius: 18rpx;
                .align(center);
                background: #e02e42;
                z-index: 20;
            }
        }
    }
}
.list{
    padding: 20rpx 20rpx;
    .m_item{
        padding: 20rpx 20rpx;
        display: flex;
        // border-bottom: 1px solid #eee;
        margin-bottom: 10rpx;
        background: #fff;
        border-radius: 10rpx;
        box-shadow: 0 0 5rpx #ccc;
        .avator{
            width: 60rpx;
            height: 60rpx;
            margin-right: 20rpx;
            image{
                width: 100%;
                height: 100%;
            }
        }
        .detail{
            flex: 1;
            .align(v-center);
            em{
                font-size: 24rpx;
                &.check{
                    color: @mainColor;
                }
                &.del {
                    color:  red;
                }
                &.refused {
                    color: #999;
                }
            }
        }
    }
}

.add_check{
    width: 600rpx;
    height: 350rpx;
    background: #fff;
    border-radius: 15rpx;
    .title{
        flex-shrink: 0;
        width: 100%;
        height: 90rpx;
        border-bottom: 1px solid #ddd;
        display: flex;
        align-items: center;
        i{
            color: #999;
            width: 80rpx;
        }
        p{
            font-weight: bold;
            flex: 1;
            text-align: center;
        }
    }
    .content{
        padding: 0 50rpx;
        margin-top: 30rpx;
    }
    .btns{
        display: flex;
        justify-content: center;
        margin-top: 50rpx;
        .btn{
            width: 240rpx;
            height: 80rpx;
            border-radius: 10rpx;
            .align(center);
            color: #fff;
            background: #3c69e5;
            &:first-child{
                margin-right: 40rpx;
                background: @mainColor;
            }

        }
    }
}

</style>